home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / infoserv / gopher / Unix / GopherTools / newitems.pl.Z / newitems.pl
Encoding:
Perl Script  |  1994-08-10  |  3.9 KB  |  178 lines

  1. #/usr/local/bin/perl
  2. #We wanted to be able to provide a listing of new/recently changed items
  3. #in our Gopher server.  The attached script is the tool I developed to 
  4. #provide this.  It generates a link file, so the listing is a directory,
  5. #not a flat file.  The user-visible name is carried over.
  6. #
  7. #Usage:  newitems /disk/path/name -#daysold server.domain.name #port
  8. #
  9. #The link file it generates is called .newlinks in the current directory.
  10. #If you place a file called .nonew in a directory, that directory is skipped.
  11. #If you place a file called .topnew in a directory, that directory will be
  12. #listed if any new files are found in that directory or its subdirectories.
  13. #This script depends on the gopherls tool which is a link to the gopherd 
  14. #program, starting with version (approx) 1.0.  You'll obviously be needing
  15. #Perl.
  16. #
  17. #This script seems to be working for me, but hasn't been tested exhaustively.
  18. #
  19. #Dennis
  20. ## newitems, boone, 09/30/92
  21. # Generate a link file listing new items in the last n days
  22. # Copyright (C) 1992, Trustees of Michigan State University
  23. #
  24. # Modifications:
  25. # 09/30/92 Boone      Initial coding
  26. # 10/01/92 Boone      Added ability to exclude individual directories
  27. # End Modifications
  28.  
  29. # Extract path part of pathname
  30.  
  31. sub path
  32. {
  33.     local($path) = @_;
  34.     local($pathsep) = rindex($path, "/");
  35.     if ($pathsep < 0)
  36.     {
  37.         return "";
  38.     }
  39.     else
  40.     {
  41.         $wanted = substr($path, 0, $pathsep+1);
  42.         return $wanted;
  43.     }
  44. }
  45.  
  46. # Extract filename part of pathname
  47.  
  48. sub base
  49. {
  50.     local($path) = @_;
  51.     local($pathsep) = rindex($path, "/");
  52.     if ($pathsep < 0)
  53.     {
  54.         return $path;
  55.     }
  56.     else
  57.     {
  58.         $wanted = substr($path, $pathsep+1);
  59.         return $wanted;
  60.     }
  61. }
  62.  
  63. # Determine whether this item is part of a subtree which wants to be
  64. # announced as a single new entry if any file in it has changed.  <pant>
  65. # Example: /news/newspapers/state_news contains a file called .topnew,
  66. # indicating that any file in the state news subtree will cause the
  67. # /news/newspapers/state_news entry to be listed once in the new items
  68. # list.
  69.  
  70. sub skip
  71. {
  72.     local(@args) = @_;
  73.     foreach $i (@topnew)
  74.     {
  75.         if (index($args[0], $i) == 0)
  76.         {
  77.             $toplist{$i} = $args[0];
  78.             return 1;
  79.         }
  80.         else
  81.         {
  82.         }
  83.     } 
  84.     return 0;
  85. }
  86.  
  87. # Write a link file entry
  88.  
  89. sub writelink
  90. {
  91.     local($line, $path) = @_;
  92.     local(@fields) = split(/\t/, $line);
  93.     local($name) = substr($fields[0], 1);
  94.     local($type) = substr($fields[0], 0, 1);
  95.     print LF "Name=$name\nType=$type\nPath=$path\nHost=$host\nPort=$port\n\n";
  96. }
  97.  
  98. # The real code
  99.  
  100. $[ = 0;
  101. if ($#ARGV < 3)
  102. {
  103.     print STDERR "Usage: newitems  path days host port\n";
  104.     exit(1);
  105. }
  106.  
  107. open(LF, ">.newlinks") || die "Unable to create .newlinks\n";
  108. chdir($ARGV[0]);
  109. chop($here = `pwd`);
  110. $days = $ARGV[1];
  111. $host = $ARGV[2];
  112. $port = $ARGV[3];
  113.  
  114. @topnew = `find . -name .topnew -print`;
  115. foreach $i (@topnew)
  116. {
  117.     $i = &path($i);
  118. }
  119. open(NF, "find . -mtime $days -type f -print | grep -v .cap |") || 
  120.     die "Unable to spawn find\n";
  121. while (<NF>)
  122. {
  123.     next if /\.cap/;
  124.     next if /\.ts/;
  125.     next if /\.bin/;
  126.     chop;
  127.     $path = &path($_);
  128.     $base = &base($_);
  129.     next if ($base =~ /^\./);
  130.     chdir($path) || die "chdir($path) died: $!\n";
  131.     if (! -f ".nonew")
  132.     {
  133.         @lines = `/admin/bin/gopherls .`;
  134.         foreach $i (@lines)
  135.         {
  136.             chop($i); chop($i);
  137.             @fields = split(/\t/, $i);
  138.             $thisname = substr($fields[1], 2);
  139.             if ($thisname eq $base)
  140.             {
  141.                 if (! &skip($_))
  142.                 {
  143.                     &writelink($i, $_);
  144.                 }
  145.             }
  146.             undef(@fields);
  147.         }
  148.         undef(@lines);
  149.     }
  150.     chdir($here) || die "chdir($here) died: $!\n";
  151. }
  152. foreach $i (keys %toplist)
  153. {
  154.     chop($i);
  155.     $base = &base($i);
  156.     chdir($i) || die "chdir($path) died: $!\n";
  157.     chdir("..") || die "chdir(..) died: $!\n";
  158.     @lines = `/usr/local/etc/gopherls .`;
  159.     foreach $j (@lines)
  160.     {
  161.         chop($j); chop($j);
  162.         @fields = split(/\t/, $j);
  163.         $thisname = substr($fields[1], 2);
  164.         if ($thisname eq $base)
  165.         {
  166.             &writelink($j, $i);
  167.         }
  168.         undef(@fields);
  169.     }
  170.     undef(@lines);
  171.     chdir($here) || die "chdir($here) died: $!\n";
  172. }
  173. close(NF);
  174. close(LF);
  175. --
  176.  == If you choose not to decide, you still have made a choice. ==
  177.  
  178.